package org.wmq.shopmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.shopmanager.mapper.OrderStatisticMapper;
import org.wmq.shopmanager.service.OrderStatisticService;
import org.wmq.shopmodel.DTO.product.OrderStatisticQueryDTO;
import org.wmq.shopmodel.Entity.product.OrderStatistic;
import org.wmq.shopmodel.VO.product.OrderStatisticVO;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OrderStatisticServiceImpl extends ServiceImpl<OrderStatisticMapper, OrderStatistic> implements OrderStatisticService {
    private final OrderStatisticMapper orderstatisticMapper;

    @Override
    public OrderStatisticVO getTotalAmountByDate(OrderStatisticQueryDTO orderStatisticQueryDTO) {
        LocalDate beginDate = orderStatisticQueryDTO.getBeginDate();
        LocalDate endDate = orderStatisticQueryDTO.getEndDate();
        LambdaQueryWrapper<OrderStatistic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(OrderStatistic::getOrderDate,OrderStatistic::getTotalAmount);
        if (beginDate != null) {
            queryWrapper.ge(OrderStatistic::getOrderDate, beginDate);
        }
        if (endDate != null){
            queryWrapper.le(OrderStatistic::getOrderDate,endDate);
        }
        List<OrderStatistic> list = this.list(queryWrapper);
        List<String> dateList = new ArrayList<>();
        for (OrderStatistic orderStatistic :list){
            dateList.add(orderStatistic.getOrderDate().toString());
        }
        List<BigDecimal> amountList = list.stream().map(OrderStatistic::getTotalAmount).toList();
        OrderStatisticVO result = new OrderStatisticVO();
        result.setAmountList(amountList);
        result.setDateList(dateList);
        return result;

    }
}
